//
//  CRBRotateBoxCustomBrandVC.m
//  ChampsARotate
//
//  Created by Login.Fan on 2018/11/1.
//  Copyright © 2018 Logan.Fan. All rights reserved.
//

#import "CRBRotateBoxCustomBrandVC.h"
#import "CRBRotateBoxCustomBrandSectionView.h"
#import "CRBRotateBoxCustomItemCell.h"

#import "CRBRotateBoxCustomChooseManage.h"
#import "CRBBrandListModel.h"
#import "CRBBoxHomeHttp.h"

#import "CRBRotateBoxCustomSeriesVC.h"
#import "CRBRotateBoxCustomAddVC.h"

// customnavigition and pop
#import "CRBCustomAddPopView.h"
#import "CRBCustomAddNavigitionView.h"
#import "TheXXSystemAlert.h"
@interface CRBRotateBoxCustomBrandVC ()
<
    UITableViewDelegate, UITableViewDataSource
>
@property (nonatomic, strong) UITableView *tableView; // table
@property (nonatomic, strong) NSMutableArray<NSString *> *dataSource; // 索引<A-Z>
@property (nonatomic, strong) BrandIndexModel *brandDataSource;
@property (nonatomic, strong) BrandInfoModel *selectedBrandInfoModel;

@property (nonatomic, strong) CRBCustomAddPopView *popView;
@property (nonatomic, strong) CRBCustomAddNavigitionView *customNavigiView;
@end

@implementation CRBRotateBoxCustomBrandVC

#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.baseNavigationView setHiddenNav:YES VC:self];

    [self initUI];
    [self initData];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:animated];}

- (void)dealloc {}

#pragma mark - 设置界面
- (void)initUI {
        __weak __typeof(self)weakSelf = self;

    // 自定义导航栏
    _customNavigiView = [CRBCustomAddNavigitionView new];
    _customNavigiView.title.text = @"品牌";
    [self.view addSubview:_customNavigiView];
    [_customNavigiView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        }else{
            make.top.equalTo(self.view.mas_top).offset(20);
        }
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(44);
    }];
    // 底部pop箭头视图
    _popView = [CRBCustomAddPopView new];
    _popView.popBlock = ^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
//        [weakSelf.navigationController popToRootViewControllerAnimated:YES complete:nil];
    };
    [self.view addSubview:_popView];
    [_popView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        }else{
            make.bottom.equalTo(self.view.mas_bottom).offset(0);
        }
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(64);
    }];
    
    [self initTableView];
}

- (void)initTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 60;
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.sectionIndexColor = [UIColor RGBColorWithR:51 g:51 b:51];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.tableFooterView = [UIView new];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.customNavigiView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.popView.mas_top).offset(0);
    }];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CRBRotateBoxCustomItemCell class]) bundle:nil] forCellReuseIdentifier:@"cell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CRBRotateBoxCustomBrandSectionView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:@"cell_section"];
    
    [self.tableView reloadData];
}

#pragma mark - 初始化数据
- (void)initData {
    [self fetchDataFromServers];
}

- (NSMutableArray<NSString *> *)dataSource {
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc] initWithArray:@[@"热门", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"]];
    }
    return _dataSource;
}

#pragma mark - 网络请求
- (void)fetchDataFromServers {
        __weak __typeof(self)weakSelf = self;

    [CRBBoxHomeHttp requestBrandListData:^(NSURLSessionDataTask *task, id responseObject) {
        
        CRBBrandListModel *model = (CRBBrandListModel *)responseObject;
        weakSelf.brandDataSource = model.brandInfo;
        [weakSelf.tableView reloadData];
        
    } fail:^(NSURLSessionDataTask *task, NSError *error, NSString *code, NSString *msg) {
        [TheXXSystemAlert alertFrom:self alertTitle:[NSString stringWithFormat:@"%@",msg] defaultTitle:@"确定" actionType:UIAlertActionStyleDefault defaultHandler:^{
            NSLog(@"code:%@ msg:%@",[NSString stringWithFormat:@"%@",code],[NSString stringWithFormat:@"%@",msg] );
        }];
    }];
}

#pragma mark - 代理
/** 索引*/
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return @[@"热门", @"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_brandDataSource) {
        return self.dataSource.count;
    } else {
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 40.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CRBRotateBoxCustomBrandSectionView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell_section"];
    [view refreshUIWithTitle:[self.dataSource objectAtIndex:section]];
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (!_brandDataSource) {
        return 0;
    }
    
    switch (section) {
        case 0:
            return _brandDataSource.hot.count;
            break;
        case 1:
            return _brandDataSource.A.count;
            break;
        case 2:
            return _brandDataSource.B.count;
            break;
        case 3:
            return _brandDataSource.C.count;
            break;
        case 4:
            return _brandDataSource.D.count;
            break;
        case 5:
            return _brandDataSource.E.count;
            break;
        case 6:
            return _brandDataSource.F.count;
            break;
        case 7:
            return _brandDataSource.G.count;
            break;
        case 8:
            return _brandDataSource.H.count;
            break;
        case 9:
            return _brandDataSource.I.count;
            break;
        case 10:
            return _brandDataSource.J.count;
            break;
        case 11:
            return _brandDataSource.K.count;
            break;
        case 12:
            return _brandDataSource.L.count;
            break;
        case 13:
            return _brandDataSource.M.count;
            break;
        case 14:
            return _brandDataSource.N.count;
            break;
        case 15:
            return _brandDataSource.O.count;
            break;
        case 16:
            return _brandDataSource.P.count;
            break;
        case 17:
            return _brandDataSource.Q.count;
            break;
        case 18:
            return _brandDataSource.R.count;
            break;
        case 19:
            return _brandDataSource.S.count;
            break;
        case 20:
            return _brandDataSource.T.count;
            break;
        case 21:
            return _brandDataSource.U.count;
            break;
        case 22:
            return _brandDataSource.V.count;
            break;
        case 23:
            return _brandDataSource.W.count;
            break;
        case 24:
            return _brandDataSource.X.count;
            break;
        case 25:
            return _brandDataSource.Y.count;
            break;
        case 26:
            return _brandDataSource.Z.count;
            break;
        default:
            return 0;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CRBRotateBoxCustomItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    BrandInfoModel *model = [self getBrandInfoModelWithIndexPath:indexPath];
    [cell setBrandInfoModel:model];
    
    [cell cellSelectes:([CRBRotateBoxCustomChooseManage shareManager].brandIndexPath == indexPath)];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([CRBRotateBoxCustomChooseManage shareManager].brandIndexPath == indexPath && [CRBRotateBoxCustomChooseManage shareManager].seriesIndexPath) {
        // 这个情况是你已经选择过系列了，所以点了相同的品牌就不继续跳转选品牌了
        [self returnAddWatchView];
        return;
    }
    _selectedBrandInfoModel = [self getBrandInfoModelWithIndexPath:indexPath];
    [[CRBRotateBoxCustomChooseManage shareManager] saveBrandWithIndexPath:indexPath brandModel:_selectedBrandInfoModel];
    [tableView reloadData]; // 为了有点击选中的效果，这b真懒啊

    CRBRotateBoxCustomSeriesVC *vc = [[CRBRotateBoxCustomSeriesVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
//    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - 其他方法
/** 返回添加界面*/
- (void)returnAddWatchView {
    for (UIViewController *vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:[CRBRotateBoxCustomAddVC class]]) {
            [(CRBRotateBoxCustomAddVC *)vc refreshChooseView];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popToViewController:vc animated:YES];
//                [self.navigationController popToViewController:vc animated:YES complete:nil];
            });
        }
    }
}

- (BrandInfoModel *)getBrandInfoModelWithIndexPath:(NSIndexPath *)indexPath {
    BrandInfoModel *model;
    switch (indexPath.section) {
        case 0:
            model = _brandDataSource.hot[indexPath.row];
            break;
        case 1:
            model = _brandDataSource.A[indexPath.row];
            break;
        case 2:
            model = _brandDataSource.B[indexPath.row];
            break;
        case 3:
            model = _brandDataSource.C[indexPath.row];
            break;
        case 4:
            model = _brandDataSource.D[indexPath.row];
            break;
        case 5:
            model = _brandDataSource.E[indexPath.row];
            break;
        case 6:
            model = _brandDataSource.F[indexPath.row];
            break;
        case 7:
            model = _brandDataSource.G[indexPath.row];
            break;
        case 8:
            model = _brandDataSource.H[indexPath.row];
            break;
        case 9:
            model = _brandDataSource.I[indexPath.row];
            break;
        case 10:
            model = _brandDataSource.J[indexPath.row];
            break;
        case 11:
            model = _brandDataSource.K[indexPath.row];
            break;
        case 12:
            model = _brandDataSource.L[indexPath.row];
            break;
        case 13:
            model = _brandDataSource.M[indexPath.row];
            break;
        case 14:
            model = _brandDataSource.N[indexPath.row];
            break;
        case 15:
            model = _brandDataSource.O[indexPath.row];
            break;
        case 16:
            model = _brandDataSource.P[indexPath.row];
            break;
        case 17:
            model = _brandDataSource.Q[indexPath.row];
            break;
        case 18:
            model = _brandDataSource.R[indexPath.row];
            break;
        case 19:
            model = _brandDataSource.S[indexPath.row];
            break;
        case 20:
            model = _brandDataSource.T[indexPath.row];
            break;
        case 21:
            model = _brandDataSource.U[indexPath.row];
            break;
        case 22:
            model = _brandDataSource.V[indexPath.row];
            break;
        case 23:
            model = _brandDataSource.W[indexPath.row];
            break;
        case 24:
            model = _brandDataSource.X[indexPath.row];
            break;
        case 25:
            model = _brandDataSource.Y[indexPath.row];
            break;
        case 26:
            model = _brandDataSource.Z[indexPath.row];
            break;
        default:
            break;
    }
    return model;
}

@end
